#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/input.h> //交叉编译器中的头文件，不是内核的
#include <string.h>
#include <poll.h>


struct key_report{
	int code;
	int value;
};

int main(void)
{
	int fd;
	int ret;
	char buff[256];
	struct pollfd fds[2];
	struct key_report event;
	fd = open("/dev/key_drv",O_RDWR);
	if(fd < 0)
	{
		perror("open");
		exit(1);
	}

	fds[0].fd = fd;
	fds[0].events = POLLIN;

	fds[1].fd = 0;
	fds[1].events = POLLIN;

	while(1)
	{
		ret = poll(fds, sizeof(fds)/sizeof(fds[0]), -1);
		if(ret > 0)
		{
			//读取按键
			if(fds[0].revents == POLLIN)
			{
				//1.读取按键
				read(fd, &event, sizeof(event));
				switch(event.code)
				{
					case KEY_A:
					{
						if(event.value == 0)
							printf("KEY_A----->pressed\n");
						else
							printf("KEY_A----->released\n");
					}
						break;
					case KEY_B:
					{
						if(event.value == 0)
							printf("KEY_B----->pressed\n");
						else
							printf("KEY_B----->released\n");
					}
						break;
					case KEY_C:
					{
						if(event.value == 0)
							printf("KEY_C----->pressed\n");
						else
							printf("KEY_C----->released\n");
					}
						break;
					default:
						break;
				}
			}

			//读取标准输入
			if(fds[1].revents == POLLIN)
			{
				//2.读取标准输入
				memset(buff, 0x00, sizeof(buff));
				read(0, buff, 256);
				printf("buff=%s\n", buff);
			}
		}
	}
	close(fd);
	return 0;
}
